package io.renren.modules.job.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.utils.Constant;
import io.renren.common.utils.Query;
import io.renren.modules.dangan.entity.DaDjgllEntity;
import io.renren.modules.dangan.service.DaDjgllService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("djgllUpdateSqlFileTask")
@Slf4j
public class DjgllUpdateSqlFileTask implements ITask {
    @Autowired
    private DaDjgllService daDjgllService;

    @Override
    public void run(String params) {
        File file = new File(params);
        List<String> updateSqls = new ArrayList<>();
        daDjgllExtentBind("C", "ZDBSM", updateSqls);
        daDjgllExtentBind("E", "XMGUID", updateSqls);
        log.info("更新总条数："+updateSqls.size());
        if(CollectionUtil.isNotEmpty(updateSqls)){
            FileUtil.appendLines(updateSqls,file,"UTF-8");
        }
    }

    private void daDjgllExtentBind(String flh, String field, List<String> updateSqls) {
        Map<String, Object> paramsMap = new HashMap<>();

        paramsMap.put(Constant.LIMIT, "100");
        paramsMap.put(Constant.PAGE, "1");
        IPage<DaDjgllEntity> page = daDjgllService.page(new Query<DaDjgllEntity>().getPage(paramsMap)
                , new QueryWrapper<DaDjgllEntity>().eq("PFLH", flh)
                        .isNotNull(field)
        );
        log.info("分类号："+flh+"字段："+field);
        log.info("查询结果："+page.getPages()+"页");
        updateSql(page.getRecords(), updateSqls);
        while (page.getCurrent() <= page.getPages()) {
            paramsMap.put(Constant.PAGE, ((int) page.getCurrent() + 1) + "");
            page = daDjgllService.page(
                    new Query<DaDjgllEntity>().getPage(paramsMap)
                    , new QueryWrapper<DaDjgllEntity>().eq("PFLH", flh)
                            .isNotNull(field));
            updateSql(page.getRecords(), updateSqls);
            log.info("当前进度："+page.getCurrent()+"  totalPage:"+ page.getPages());
        }
    }

    private void updateSql(List<DaDjgllEntity> daDjgllEntities, List<String> updateSqls) {
        if (CollectionUtil.isNotEmpty(daDjgllEntities)) {
            daDjgllEntities.forEach(daDjgllEntity -> {
                if (StringUtils.isNotEmpty(daDjgllEntity.getXmguid())) {
                    String updateSql = "update DA_DJGLL SET XMGUID='" + daDjgllEntity.getXmguid() + "' WHERE NBXH='" + daDjgllEntity.getNbxh() + "'";
                    updateSqls.add(updateSql);
                }
                if (StringUtils.isNotEmpty(daDjgllEntity.getZdbsm())) {
                    String updateSql = "update DA_DJGLL SET ZDBSM='" + daDjgllEntity.getZdbsm() + "' WHERE NBXH='" + daDjgllEntity.getNbxh() + "'";
                    updateSqls.add(updateSql);
                }
            });
        }

    }
}
